//
//  YYESettingViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/5.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYESettingViewController.h"
#import "YYESettingTableViewCell.h"
// 问题反馈
#import "YYEProbleViewController.h"

@interface YYESettingViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) YYEBaseTableView *tableView;

@end

@implementation YYESettingViewController

static NSString * settingCell = @"setCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"设置";
    
    [self configUI];
    
}

- (void)configUI {
    
    [self.view addSubview:self.tableView];
    
    self.tableView.backgroundColor = GeneralBackColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[YYESettingTableViewCell class] forCellReuseIdentifier:settingCell];
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.top).offset(10);
        make.left.right.bottom.equalTo(self.view);
    }];
    
}

- (YYEBaseTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[YYEBaseTableView alloc]initwithDelegate:self];
    }
    return _tableView;
    
}

#pragma mark -- UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YYESettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:settingCell];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSArray *names = @[@"问题反馈",@"联系客服",@"清理缓存"];
    cell.nameLabel.text = names[indexPath.row];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 55;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        case 0:
        {
            // 问题反馈
            YYEProbleViewController *probleVC = [[YYEProbleViewController alloc]init];
            [self.navigationController pushViewController:probleVC animated:YES];
            
        }
            break;
        case 1:
        {
            // 联系客服
            NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"10086"];
            UIWebView * callWebview = [[UIWebView alloc] init];
            [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
            [self.view addSubview:callWebview];
            
            
        }
            break;
        case 2:
        {
            // 清理缓存
            
            
        }
            break;
        default:
            break;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 112)];
    contentView.backgroundColor = GeneralBackColor;
    
    UIButton *logOutBtn = [[UIButton alloc]init];
    [logOutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [logOutBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    logOutBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [logOutBtn setBackgroundColor:GeneralBlueColor];
    [logOutBtn addTarget:self action:@selector(logoutClicked) forControlEvents:UIControlEventTouchUpInside];
    logOutBtn.layer.cornerRadius = 5;
    [contentView addSubview:logOutBtn];
    
    [logOutBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView.top).offset(70);
        make.left.equalTo(contentView.left).offset(33);
        make.right.equalTo(contentView.right).offset(-33);
        make.height.equalTo(42);
    }];
    
    return contentView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 112;
    
}

// 退出登录
- (void)logoutClicked {
    
    REMOVEDATAFROMUSERDEFAULT(@"role");
    REMOVEDATAFROMUSERDEFAULT(@"uid");
    REMOVEDATAFROMUSERDEFAULT(@"username");
    REMOVEDATAFROMUSERDEFAULT(@"token");
    [LoginModel shareLogin].role = @"";
    [LoginModel shareLogin].uid = @"";
//    [LoginModel shareLogin].username = @"";
    [LoginModel shareLogin].token = @"";
    
    if ([self.delegate respondsToSelector:@selector(logOut)]) {
        [self.delegate logOut];
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
